package com.ruoyi.system.service.impl;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Room;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ReservationMapper;
import com.ruoyi.system.domain.Reservation;
import com.ruoyi.system.service.IReservationService;

/**
 * 房间预约Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-18
 */
@Service
public class ReservationServiceImpl implements IReservationService 
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询房间预约
     * 
     * @param id 房间预约主键
     * @return 房间预约
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return reservationMapper.selectReservationById(id);
    }

    /**
     * 查询房间预约列表
     * 
     * @param reservation 房间预约
     * @return 房间预约
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        System.out.println(reservation);
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增房间预约
     * 
     * @param reservation 房间预约
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        reservation.setCreateTime(DateUtils.getNowDate());
        return reservationMapper.insertReservation(reservation);
    }

    /**
     * 修改房间预约
     * 
     * @param reservation 房间预约
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        reservation.setUpdateTime(DateUtils.getNowDate());
        return reservationMapper.updateReservation(reservation);
    }

    /**
     * 批量删除房间预约
     * 
     * @param ids 需要删除的房间预约主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return reservationMapper.deleteReservationByIds(ids);
    }

    /**
     * 删除房间预约信息
     * 
     * @param id 房间预约主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return reservationMapper.deleteReservationById(id);
    }
//查询房间使用率
    @Override
    public int getRoomRate(String date, String time) {
        return reservationMapper.getRoomRate(date,time);
    }
//查询该日的的预约时间段
    @Override
    public List<Integer> getBookedHoursManually(Long roomId, Date date) {
        Reservation reservation = new Reservation();
        reservation.setRoomId(roomId);
        reservation.setReservationDate(date);
        List<Reservation> reservations = reservationMapper.selectReservationList(reservation);

        return reservations.stream()
                .flatMap(res -> {
                    int startHour = res.getStartTime().toInstant().atZone(ZoneId.systemDefault())
                            .toLocalDateTime().getHour();
                    int endHour = res.getEndTime().toInstant().atZone(ZoneId.systemDefault())
                            .toLocalDateTime().getHour();
                    return IntStream.range(startHour, endHour).boxed();
                })
                .distinct()
                .sorted()
                .collect(Collectors.toList());
    }

}
